<?php

namespace app\admin\controller;

use think\Controller;

use think\Db;
use think\Request;
use think\File;
class Small extends Controller
{

    public function index()
    {
        $this->fetch('small/small');
    }
    
    
    public function edit()
    {


        $id = input('id');
        $small = db('to_small')->find($id);

        $img = $small['img'];
        $url = $small['url'];
        $desc = $small['desc'];

        return $this->fetch('small/edit', [
            'id' => $id,
            'img' => $img,
            'url' => $url,
            'desc' => $desc

        ]);


    }

    public function edits(){
        $id = input('id');
        // dump($id);die;
        $links = db('to_small')->find($id);

        if (request()->isPost()) {
            $data = [
                'id' => $id,
                'img' => input('img'),
                'url' => input('url'),
                'desc' => input('desc')
            ];


//            $validate = \think\Loader::validate('Links');
//            if (!$validate->check($data)) {
//                $this->error($validate->getError());
//                die;
//            }
            if (db('to_small')->update($data)) {
                $this->success('修改小广告成功',url('admin/small/index'));

            } else {
                $this->error('修改小广告失败');
            }
            return;
        }
    }

    public function add()
    {
        return view('small/add');
    }

//查询广告
    public function small_select()
    {

//        Db::table('think_user')->where('id',1)->find();
        $list = Db::name('to_small')->field(['id', 'desc', 'img','url'])->select();

        return $this->fetch('small/small', [
            'list' => $list,
        ]);
    }


//    编辑广告
    public function small_edit(Request $request)
    {
        $data = Request::instance()->post();

        $id=$data['id'];
        $file = request()->file('img');

        if($file) {
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->validate(['size' => 1500000, 'ext' => 'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');

//          var_dump($info) ;die;
        }
            if(!empty($info)){
                // 成功上传后 获取上传信息
                $a=$info->getSaveName();
                $imgp= str_replace("\\","/",$a);
                $data['img']= $imgp;
                $resd=Db::table('to_small')->where('id',$id)->update($data);
                if($resd){
                    return $this->success('更新图片成功',url('admin/small/small_select'));
                }else{
                    return $this->success('更新图片失败');
                }
            }else{
                return $this->error('图片不能为空');
            }
    }
// 添加小广告
    public function small_add(Request $request)
    {
        $file = request()->file('img');
        $data = Request::instance()->post();
        if ($file) {
            $info = $file->validate(['size' => 156780000, 'ext' => 'jpg,jpeg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
        }
        if (!empty($info)) {
            // 成功上传后 获取上传信息
            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            $img = $info->getSaveName();
            $imgs = str_replace('\\', '/', $img);

                $result = Db::table('to_small')
                    ->data(['desc' => $data['desc'],'url' => $data['url'],'img'=>$imgs]) ->insert();
                if ($result) {
                    return $this->success('添加小广告成功', url('admin/small/small_select'));
                } else {
                    return $this->error('添加小广告失败');
                }
        }
        else {
            return $this->error('图片不能为空');
        }
    }
    // 删除小广告
    public function del()
    {
        $id =input('id');
        if (db('to_small')->delete($id)) {
            $this->success('删除小广告成功', 'admin/small/small_select');
        } else {
            $this->error('删除小广告失败');
        }
        return;

        return $this->redirect('admin/small/small_select');
    }


}
